package cn.openea.perpartner.mpwx.controller;



import cn.openea.perpartner.mpwx.entity.MagmasConfig;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import cn.openea.perpartner.common.core.constant.UserConstants;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.mpwx.service.IMagmasConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * (MsgmamgConfig)表控制层
 *
 * @author makejava
 * @since 2022-06-29 15:51:14
 */
@RequestMapping("/magmas")
@Controller
public class MagmasController extends BaseController {
    private String prefix = "system/config";
    /**
     * 服务对象
     */
    @Autowired
    private IMagmasConfigService magmasConfigService;


    @RequestMapping(value = "toList")
    public String toList(Model model, HttpServletResponse response, HttpServletRequest request){
        return prefix + "/config";

    }
    /**
     * 新增参数配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    @GetMapping("/edit/{configId}")
    public String edit(@PathVariable("configId") Long configId, ModelMap mmap)
    {
        mmap.put("config", magmasConfigService.getById(configId));
        return prefix + "/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated MagmasConfig config)
    {
        if (UserConstants.CONFIG_KEY_NOT_UNIQUE.equals(magmasConfigService.checkEventKeyUnique(config)))
        {
            return error("修改关键字'" + config.getEventkey() + "'失败，关键字已存在");
        }

        return toAjax(magmasConfigService.updateById(config));
    }
    /**
     * 校验参数键名
     */
    @PostMapping("/checkEventKeyUnique")
    @ResponseBody
    public String checkEventKeyUnique(MagmasConfig config)
    {
        return magmasConfigService.checkEventKeyUnique(config);
    }
    /**
     * 分页查询所有数据
     */
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "当前页数据量")
    })
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo selectAll(String pageNum, String pageSize) {
        startPage();
        List<MagmasConfig> list = magmasConfigService.selectList();
        return getDataTable(list);
    }

    /**
     * 新增数据
     *
     * @param magmasConfig 实体对象
     * @return 新增结果
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@Validated  MagmasConfig magmasConfig) {
        if (UserConstants.CONFIG_KEY_NOT_UNIQUE.equals(magmasConfigService.checkEventKeyUnique(magmasConfig)))
        {
            return error("新增关键字'" + magmasConfig.getEventkey() + "'失败，关键字已存在");
        }
        return toAjax(magmasConfigService.save(magmasConfig));
    }

    /**
     * 删除数据
     *
     * @param ids 主键
     * @return 删除结果
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        magmasConfigService.deleteConfigByIds(ids);
        return success();
    }
}

